package br.ufrn.btest.criterias;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class EachChoiceCriteria extends Criteria {

	public EachChoiceCriteria(List<List<String>> parametersInputValues) {
		super(parametersInputValues);
	}

	/**
	 * Create a list of combinations using the each-choice testing criteria.
	 * Each element on the list is a list representing a combination of blocks.
	 * The number of combinations on this criteria is equal to the number of
	 * blocks of the characteristic with the most blocks. The combination is
	 * made adding one block of each characteristic according to a index. When
	 * the index is bigger than the number of blocks of a characteristic, the
	 * last block of that characteristic is added to the combination.
	 */
	@Override
	public List<List<String>> getCombinations() {
		List<List<String>> parametersInputValues = getParametersInputValues();
		List<List<String>> combinations = new ArrayList<List<String>>();

		int maxNumberOfValues = getMaxNumberOfValues(parametersInputValues);

		for (int valueIndex = 0; valueIndex < maxNumberOfValues; valueIndex++) {
			List<String> combination = new ArrayList<String>();
			
			for (List<String> parameter : parametersInputValues) {
				int numberOfValues = parameter.size();
				if (valueIndex < numberOfValues) {
					combination.add(parameter.get(valueIndex));
				} else {
					combination.add(getLastValue(parameter));
				}
			}

			Collections.sort(combination);

			combinations.add(combination);
		}

		return combinations;
	}

	private int getMaxNumberOfValues(List<List<String>> parameters) {
		int maxNumberOfValues = 0;

		for(List<String> parameterInputValues : parameters) {
			int numberOfValues = parameterInputValues.size();
			if(numberOfValues > maxNumberOfValues) {
				maxNumberOfValues = numberOfValues;
			}
		}

		return maxNumberOfValues;
	}

	private String getLastValue(List<String> parameter) {
		return parameter.get(parameter.size() - 1);
	}

}
